local skel = fk.CreateSkill {
  name = "lb__zhuying",
  max_branches_use_time = {
    ["slash"] = {
      [Player.HistoryRound] = 1
    },
    ["shade"] = {
      [Player.HistoryRound] = 1
    },
    ["hidden"] = {
      [Player.HistoryRound] = 1
    },
  }
}

local LB_U = require "packages.lb_kill_utility.utility"

Fk:loadTranslationTable {
  ["lb__zhuying"] = "逐影",
  [":lb__zhuying"] = "每轮每项各限一次：" ..
      "1.当你受到伤害后，你可重铸一张牌为不计入次数与手牌上限的【杀】；" ..
      "2.当你对一名角色造成伤害后，你可重铸其一张牌为【影】；" ..
      "3.成为过上一项的目标回合开始时，你可隐匿至当前回合结束。",

  ["#lb__zhuying_1"] = "逐影：你可重铸一张牌为不计入次数与手牌上限的【杀】",
  ["#lb__zhuying_2"] = "逐影：你可重铸 %dest 一张牌为【影】",
  ["#lb__zhuying_3"] = "逐影：你可隐匿至当前回合结束（当前回合角色：%dest）",

  ["@@lb__zhuying-inhand"] = "逐影",
  ["@[player]lb__zhuying"] = "逐影",
  ["@[skill_choices]lb__zhuying"] = "逐影",

  ["$lb__zhuying1"] = "时机，转瞬即逝。",
  ["$lb__zhuying2"] = "夜色，如影随形。",
}

skel:addEffect(fk.Damaged, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and not player:isNude()
        and skel:withinBranchTimesLimit(player, "slash", Player.HistoryRound)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(player, {
      skill_name = skel.name,
      prompt = "#lb__zhuying_1",
      cancelable = true,
      include_equip = true,
      min_num = 1,
      max_num = 1,
    })
    if #cards > 0 then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "slash")
    local cards = event:getCostData(self).cards
    LB_U.CertainlyRecastCard(cards, player, skel.name, "slash", false, "@@lb__zhuying-inhand")
  end,
}, { check_skill_limit = true })

skel:addEffect(fk.Damage, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and not data.to:isNude()
        and skel:withinBranchTimesLimit(player, "shade", Player.HistoryRound)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__zhuying_2::" .. data.to.id })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "shade")
    local room = player.room
    local card = room:askToChooseCard(player, {
      target = data.to,
      skill_name = skel.name,
      prompt = "#lb__zhuying_2::" .. data.to.id,
      flag = "he",
    })
    LB_U.CertainlyRecastCard({ card }, data.to, skel.name, "shade")
    room:addTableMarkIfNeed(player, "@[player]lb__zhuying", data.to.id)
  end,
}, { check_skill_limit = true })

skel:addEffect(fk.TurnStart, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and table.contains(player:getTableMark("@[player]lb__zhuying"), target.id)
        and skel:withinBranchTimesLimit(player, "hidden", Player.HistoryRound)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__zhuying_3::" .. target.id })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "hidden")
    LB_U.enterHidden(player, "lb__moze_yinni")
    local Turn = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if Turn then
      Turn:addCleaner(function()
        if player:getMark("__hidden_general") ~= 0 then --只判断主将吧
          LB_U.GeneralAppear(player, skel.name)
        end
      end)
    end
  end,
}, { check_skill_limit = true })

skel:addEffect("maxcards", {
  exclude_from = function(self, player, card)
    if card:getMark("@@lb__zhuying-inhand") ~= 0 then
      return true
    end
  end,
})

skel:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    if card:getMark("@@lb__zhuying-inhand") ~= 0 then
      return true
    end
  end,
})

skel:addEffect(fk.PreCardUse, {
  can_refresh = function(self, event, target, player, data)
    return player == target and data.card:getMark("@@lb__zhuying-inhand") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    data.extraUse = true
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  local list = {
    ["name"] = skel.name,
    ["scope"] = Player.HistoryRound,
    ["choices"] = { { "slash" }, { "shade" }, { "hidden" } }
  }
  player.room:setPlayerMark(player, "@[skill_choices]lb__zhuying", list)
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[skill_choices]lb__zhuying", 0)
end)

return skel
